package scuts.macro;

import haxe.macro.Expr;
import haxe.macro.Context;
/**
 * ...
 * @author 
 */

class Convert 
{

	public static function stringToComplexType (s:String, ?pos:Position):ComplexType
	{
		if (pos == null) pos = Context.currentPos();
		s = "{ var a:" + s + ";}";
		//trace(s);
		var e = ExtendedContext.parse(s, pos);
		return switch (e.expr) {
			case EBlock(b):
				switch (b[0].expr) {
					case EVars(v):
						v[0].type;
					default:throw "assert";
				}
			default: throw "assert";
		}
	}
	
	public static function complexTypeToType (t:ComplexType):haxe.macro.Type
	{
		var e = Parse.parse("{ var a : $0 = null; a;}", [t]);
		return Context.typeof(e);
	}
	
	public static function typeToComplexType (t:haxe.macro.Type, pos:Position):ComplexType
	{
    trace(t);
    var s = Print.typeStr( t, true);
    
    trace(s);
    
		return stringToComplexType(s, pos);
	}
	
	
	public static function exprToType (e:Expr) {
    
		return switch (e.expr) {
			case EConst(c):
				switch (c) {
					case CType(t):
						stringToComplexType(t, e.pos);
					default: throw "Expression " + Print.exprStr(e) + " is not a Type";
				}
      case EType(_, _):
        stringToComplexType(Print.exprStr(e), e.pos);
			default: throw "Expression " + Print.exprStr(e) + " is not a Type";
		}
	}
  /* from MacroTools */
  /*
  public static function exprToType(expr:Expr):Type
    {
      return 
        try switch (expr.expr) 
          {
            case EConst(c):
              switch (c) {
                case CType(s): Context.getType(s);
                default: throw expr;
              }
            default: throw expr;
          } 
        catch (e:Dynamic) 
          {
            throw "Unsupported Expression, Type expected";
          }
      
    }
  */
	
}